<template>
    <div class="askingfor-pop">
        <el-dialog
            :visible="checkrepeatShow"
            @close="colsePop"
            @open="openAsking"
            width="50%"
            title="学校查询"
        >
            <div class="askingfor-cont">
                <div class="askingfor-input">
                    <el-input v-model="message" placeholder="请输入学校名称"></el-input>
                </div>
                <div class="askingfor-btn">
                    <el-button type="primary" :loading="checkloading" @click="submitCheck">查询</el-button>
                </div>
            </div>
            <div class="askingfor-cont">
                <div class="askingfor-input">
                    <div class="schoolBoxContItemInput">
                        <el-cascader
                            clearable
                            style="width:100% !important"
                            size="large"
                            placeholder="请选择地区"
                            :options="options"
                            v-model="selectedOptions"
                        ></el-cascader>
                    </div>
                </div>

                <div class="askingfor-btn">
                    <el-button type="primary" :loading="checkloading" @click="submitCheck2">查询</el-button>
                </div>
            </div>
            <el-table :data="tableData" height="300" style="width: 100%" v-loading='checkloading'  element-loading-text="加载中">
                <el-table-column prop="name" label="学校名"></el-table-column>
                <el-table-column prop="peopleName" label="大区经理"></el-table-column>
                <el-table-column prop="thirdLevelDepartment" label="部门"></el-table-column>
                <el-table-column prop="recordNum" label="报备次数"></el-table-column>
                <el-table-column prop="diqu" label="地区" width="300"></el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>

<script>
import Axios from "axios";
import axios from "axios";
import { CodeToText } from "element-china-area-data";
import { regionDataPlus } from "element-china-area-data";
export default {
    name: "Checkrepeat",
    components: {},
    props: {
        checkrepeatShow: false,
        checkloading: false,
        isLoadingSuccess: Boolean
    },
    data() {
        return {
            message: "",
            tableData: [],
            // 地区
            options: regionDataPlus,
            selectedOptions: [],
            checkloading: false
        };
    },
    methods: {
        colsePop() {
            this.message = "";
            this.tableData = [];
            this.$emit("colsePop", this.message);
        },
        openAsking(val) {
            this.$emit("openAsking", this.checkrepeatShow);
        },
        submitCheck2() {
            this.checkloading = true;
            let province = this.selectedOptions[0];
            let city = this.selectedOptions[1];
            let area = this.selectedOptions[2];
            if (province & city & area) {
                this.$api.school
                    .querySchools({
                        province: province,
                        city: city,
                        area: area
                    })
                    .then(res => {
                        this.$api.dropdown.deptLevel3().then(res2 => {
                            if (res2.result == 1) {
                                res.schoolsQuerys.forEach(v => {
                                    res2.departmentInfoList.forEach(j => {
                                        if (v.thirdLevelDepartment == j.id) {
                                            v.thirdLevelDepartment = j.deptName;
                                        }
                                    });
                                });
                                if (res.schoolsQuerys.length == 0) {
                                    this.$message({
                                        message: "无数据",
                                        type: "success"
                                    });
                                }
                            } else {
                                this.$message({
                                    message: "获取部门数据失败",
                                    type: "error"
                                });
                            }
                            this.checkloading = false;
                        });
                        this.tableData = res.schoolsQuerys;
                        res.schoolsQuerys.forEach(item => {
                            if (
                                item.province == null ||
                                item.province == "null"
                            ) {
                                item.diqu = "";
                            } else if (
                                (item.province != null && item.city == null) ||
                                (item.province != "null" && item.city == "null")
                            ) {
                                item.diqu = CodeToText[item.province];
                            } else if (
                                (item.province != null &&
                                    item.city != null &&
                                    item.area == null) ||
                                (item.province != "null" &&
                                    item.city != "null" &&
                                    item.area == "null")
                            ) {
                                item.diqu =
                                    CodeToText[item.province] +
                                    CodeToText[item.city];
                            } else if (
                                (item.province != null &&
                                    item.city != null &&
                                    item.area != null) ||
                                (item.province != "null" &&
                                    item.city != "null" &&
                                    item.area != "null")
                            ) {
                                item.diqu =
                                    CodeToText[item.province] +
                                    CodeToText[item.city] +
                                    CodeToText[item.area];
                            }
                        });
                    });
            }
            if (
                (province != "") &
                (city == "") &
                (typeof area == "undefined")
            ) {
                this.$api.school
                    .querySchools({ province: province })
                    .then(res => {
                        this.$api.dropdown.deptLevel3().then(res2 => {
                            if (res2.result == 1) {
                                res.schoolsQuerys.forEach(v => {
                                    res2.departmentInfoList.forEach(j => {
                                        if (v.thirdLevelDepartment == j.id) {
                                            v.thirdLevelDepartment = j.deptName;
                                        }
                                    });
                                });
                                if (res.schoolsQuerys.length == 0) {
                                    this.$message({
                                        message: "无数据",
                                        type: "success"
                                    });
                                }
                            } else {
                                this.$message({
                                    message: "获取部门数据失败",
                                    type: "error"
                                });
                            }
                            this.checkloading = false;
                        });
                        this.tableData = res.schoolsQuerys;
                        res.schoolsQuerys.forEach(item => {
                            if (
                                item.province == null ||
                                item.province == "null"
                            ) {
                                item.diqu = "";
                            } else if (
                                (item.province != null && item.city == null) ||
                                (item.province != "null" && item.city == "null")
                            ) {
                                item.diqu = CodeToText[item.province];
                            } else if (
                                (item.province != null &&
                                    item.city != null &&
                                    item.area == null) ||
                                (item.province != "null" &&
                                    item.city != "null" &&
                                    item.area == "null")
                            ) {
                                item.diqu =
                                    CodeToText[item.province] +
                                    CodeToText[item.city];
                            } else if (
                                (item.province != null &&
                                    item.city != null &&
                                    item.area != null) ||
                                (item.province != "null" &&
                                    item.city != "null" &&
                                    item.area != "null")
                            ) {
                                item.diqu =
                                    CodeToText[item.province] +
                                    CodeToText[item.city] +
                                    CodeToText[item.area];
                            }
                        });
                    });
            }
            if (
                (province == "") &
                (typeof city == "undefined") &
                (typeof area == "undefined")
            ) {
                this.$api.school.querySchools().then(res => {
                    this.$api.dropdown.deptLevel3().then(res2 => {
                        if (res2.result == 1) {
                            res.schoolsQuerys.forEach(v => {
                                res2.departmentInfoList.forEach(j => {
                                    if (v.thirdLevelDepartment == j.id) {
                                        v.thirdLevelDepartment = j.deptName;
                                    }
                                });
                            });
                            if (res.schoolsQuerys.length == 0) {
                                this.$message({
                                    message: "无此学校名",
                                    type: "success"
                                });
                            }
                        } else {
                            this.$message({
                                message: "获取部门数据失败",
                                type: "error"
                            });
                        }
                        this.checkloading = false;
                    });
                    this.tableData = res.schoolsQuerys;
                    res.schoolsQuerys.forEach(item => {
                        if (item.province == null || item.province == "null") {
                            item.diqu = "";
                        } else if (
                            (item.province != null && item.city == null) ||
                            (item.province != "null" && item.city == "null")
                        ) {
                            item.diqu = CodeToText[item.province];
                        } else if (
                            (item.province != null &&
                                item.city != null &&
                                item.area == null) ||
                            (item.province != "null" &&
                                item.city != "null" &&
                                item.area == "null")
                        ) {
                            item.diqu =
                                CodeToText[item.province] +
                                CodeToText[item.city];
                        } else if (
                            (item.province != null &&
                                item.city != null &&
                                item.area != null) ||
                            (item.province != "null" &&
                                item.city != "null" &&
                                item.area != "null")
                        ) {
                            item.diqu =
                                CodeToText[item.province] +
                                CodeToText[item.city] +
                                CodeToText[item.area];
                        }
                    });
                });
            }
            if (
                (typeof province == "undefined") &
                (typeof city == "undefined") &
                (typeof area == "undefined")
            ) {
                this.$api.school.querySchools().then(res => {
                    this.$api.dropdown.deptLevel3().then(res2 => {
                        if (res2.result == 1) {
                            res.schoolsQuerys.forEach(v => {
                                res2.departmentInfoList.forEach(j => {
                                    if (v.thirdLevelDepartment == j.id) {
                                        v.thirdLevelDepartment = j.deptName;
                                    }
                                });
                            });
                            if (res.schoolsQuerys.length == 0) {
                                this.$message({
                                    message: "无数据",
                                    type: "success"
                                });
                            }
                        } else {
                            this.$message({
                                message: "获取部门数据失败",
                                type: "error"
                            });
                        }
                        this.checkloading = false;
                    });
                    this.tableData = res.schoolsQuerys;
                    res.schoolsQuerys.forEach(item => {
                        if (item.province == null || item.province == "null") {
                            item.diqu = "";
                        } else if (
                            (item.province != null && item.city == null) ||
                            (item.province != "null" && item.city == "null")
                        ) {
                            item.diqu = CodeToText[item.province];
                        } else if (
                            (item.province != null &&
                                item.city != null &&
                                item.area == null) ||
                            (item.province != "null" &&
                                item.city != "null" &&
                                item.area == "null")
                        ) {
                            item.diqu =
                                CodeToText[item.province] +
                                CodeToText[item.city];
                        } else if (
                            (item.province != null &&
                                item.city != null &&
                                item.area != null) ||
                            (item.province != "null" &&
                                item.city != "null" &&
                                item.area != "null")
                        ) {
                            item.diqu =
                                CodeToText[item.province] +
                                CodeToText[item.city] +
                                CodeToText[item.area];
                        }
                    });
                });
            }
            if ((province != "") & (city != "") & (area == "")) {
                this.$api.school
                    .querySchools({
                        province: province,
                        city: city,
                        area: area
                    })
                    .then(res => {
                        this.tableData = res.schoolsQuerys;
                        res.schoolsQuerys.forEach(item => {
                            this.$api.dropdown.deptLevel3().then(res2 => {
                                if (res2.result == 1) {
                                    res.schoolsQuerys.forEach(v => {
                                        res2.departmentInfoList.forEach(j => {
                                            if (
                                                v.thirdLevelDepartment == j.id
                                            ) {
                                                v.thirdLevelDepartment =
                                                    j.deptName;
                                            }
                                        });
                                    });
                                    if (res.schoolsQuerys.length == 0) {
                                        this.$message({
                                            message: "无数据",
                                            type: "success"
                                        });
                                    }
                                } else {
                                    this.$message({
                                        message: "获取部门数据失败",
                                        type: "error"
                                    });
                                }
                                this.checkloading = false;
                            });
                            if (
                                item.province == null ||
                                item.province == "null"
                            ) {
                                item.diqu = "";
                            } else if (
                                (item.province != null && item.city == null) ||
                                (item.province != "null" && item.city == "null")
                            ) {
                                item.diqu = CodeToText[item.province];
                            } else if (
                                (item.province != null &&
                                    item.city != null &&
                                    item.area == null) ||
                                (item.province != "null" &&
                                    item.city != "null" &&
                                    item.area == "null")
                            ) {
                                item.diqu =
                                    CodeToText[item.province] +
                                    CodeToText[item.city];
                            } else if (
                                (item.province != null &&
                                    item.city != null &&
                                    item.area != null) ||
                                (item.province != "null" &&
                                    item.city != "null" &&
                                    item.area != "null")
                            ) {
                                item.diqu =
                                    CodeToText[item.province] +
                                    CodeToText[item.city] +
                                    CodeToText[item.area];
                            }
                        });
                    });
            }
        },
        submitCheck() {
          this.checkloading = true
            this.$emit("submitCheck", this.message);
            if (this.isLoadingSuccess) {
                this.message = "";
            }
            this.$api.school.querySchools({ name: this.message }).then(res => {
                if (res.result == 1) {
                    this.$message({
                        message: "查询成功",
                        type: "success"
                    });
                    res.schoolsQuerys.forEach(item => {
                        if (item.province == null || item.province == "null") {
                            item.diqu = "";
                        } else if (
                            (item.province != null && item.city == null) ||
                            (item.province != "null" && item.city == "null")
                        ) {
                            item.diqu = CodeToText[item.province];
                        } else if (
                            (item.province != null &&
                                item.city != null &&
                                item.area == null) ||
                            (item.province != "null" &&
                                item.city != "null" &&
                                item.area == "null")
                        ) {
                            item.diqu =
                                CodeToText[item.province] +
                                CodeToText[item.city];
                        } else if (
                            (item.province != null &&
                                item.city != null &&
                                item.area != null) ||
                            (item.province != "null" &&
                                item.city != "null" &&
                                item.area != "null")
                        ) {
                            item.diqu =
                                CodeToText[item.province] +
                                CodeToText[item.city] +
                                CodeToText[item.area];
                        }
                    });
                    this.$api.dropdown.deptLevel3().then(res2 => {
                        if (res2.result == 1) {
                            res.schoolsQuerys.forEach(v => {
                                res2.departmentInfoList.forEach(j => {
                                    if (v.thirdLevelDepartment == j.id) {
                                        v.thirdLevelDepartment = j.deptName;
                                    }
                                });
                            });
                            if (res.schoolsQuerys.length == 0) {
                                this.$message({
                                    message: "无此学校名",
                                    type: "success"
                                });
                            }
                        } else {
                            this.$message({
                                message: "获取部门数据失败",
                                type: "error"
                            });
                        }
                    });
                    this.tableData = res.schoolsQuerys;
                    this.checkloading = false;
                    this.askingforShow = false;
                    this.isLoadingSuccess = true;
                } else {
                    this.$message({
                        message: res.msg,
                        type: "error"
                    });
                    this.checkloading = false;
                    this.isLoadingSuccess = false;
                }
                this.checkloading = false
            });
        }
    },
    watch: {},
    mounted() {}
};
</script>
<style lang='scss' scoped>
.askingfor-cont {
    width: 100%;
    margin: 0 auto;
    display: flex;
    margin-bottom: 20px;
    flex-direction: row;
    .askingfor-input {
        width: 90%;
    }
}
</style>



